/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.viewtickets;

import assignment_cp3120.dataObjects.Flight;
import assignment_cp3120.ucm.UCStartup;
import assignment_cp3120.view.MainFrame;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.sql.Date;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/**
 * View for the UCViewTickets use case
 * @author Administrator
 */
public class ViewTicketsView {
    private ViewTicketsModel model;
    
    private JPanel scrollPanel;
    private JPanel mainPanel;
    private JScrollPane scrollPane;
    
    private JLabel flight_IDLabel;
    private JLabel dep_IDLabel;
    private JLabel dest_IDLabel;
    private JLabel flight_dateLabel;
    private JLabel dep_timeLabel;
    private JLabel arr_timeLabel;
    /**
     * Default constructor, calls the Constructor(Model) with a new model
     */
    public ViewTicketsView(){
        this(new ViewTicketsModel());
    }
    /**
     * Constructor that takes a model, sets the model to the input
     * @param model
     */
    public ViewTicketsView( ViewTicketsModel model){
        this.model = model;
    }
    /**
     * Creates the view and returns the view as a panel
     * @return
     * A panel that holds all the components of this view
     */
    public JPanel createView(){
        GridLayout layout = new GridLayout(0,1);
        
        Date today = new Date(System.currentTimeMillis());
        model.loadFlightsFromDate(today);
        
        Vector<Flight> flights = model.getFlights();
        
        System.out.println(flights);
        mainPanel = new JPanel();        
        scrollPanel = new JPanel(layout);    
        
        flight_IDLabel = new JLabel("Flight_ID:");
        dep_IDLabel = new JLabel("Dept ID:");
        dest_IDLabel = new JLabel("Dest ID:");
        flight_dateLabel = new JLabel("Flight Date:");
        dep_timeLabel = new JLabel("Dept Time:");
        arr_timeLabel = new JLabel("Arr Time:");
        
        FlowLayout fl = new FlowLayout();
        JPanel rowPanel = new JPanel(fl);
        rowPanel.add(flight_IDLabel);
        rowPanel.add(dep_IDLabel);
        rowPanel.add(dest_IDLabel);
        rowPanel.add(flight_dateLabel);
        rowPanel.add(dep_timeLabel);
        rowPanel.add(arr_timeLabel);
                
        for(int i = 0; i < flights.size(); i++){
            scrollPanel.add(createRow(flights.get(i)));
        }
        scrollPane = new JScrollPane(scrollPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setPreferredSize(new Dimension(MainFrame.CENTER_PANE_WIDTH, MainFrame.CENTER_PANE_HEIGHT));
        mainPanel.setPreferredSize(new Dimension(MainFrame.CENTER_PANE_WIDTH, MainFrame.CENTER_PANE_HEIGHT));
        mainPanel.add(rowPanel);
        mainPanel.add(scrollPane);
        return mainPanel;
    }
    /**
     * A function to create a row of text fields to hold the data from a single flight
     * @param flight
     * Flight that will be shown on this row
     * @return
     * The current row is returned as a panel
     */
    public JPanel createRow(Flight flight){
        
        JTextField flight_ID = new JTextField(flight.getFlight_id());
        JTextField dep_ID = new JTextField(flight.getDep_id());
        JTextField dest_ID = new JTextField(flight.getDest_id());
        JTextField flight_date = new JTextField(flight.getFlight_date().toString());
        JTextField dep_time = new JTextField(flight.getDep_time().toString());
        JTextField arr_time = new JTextField(flight.getArr_time().toString());
        
        FlowLayout fl = new FlowLayout();
        JPanel rowPanel = new JPanel(fl);
        
        rowPanel.add(flight_ID);
        rowPanel.add(dep_ID);
        rowPanel.add(dest_ID);
        rowPanel.add(flight_date);
        rowPanel.add(dep_time);
        rowPanel.add(arr_time);        
        
        return rowPanel;
    }
    /**
     * Get the title of this view
     * @return
     * A lable with the title as its text
     */
    public JLabel getTitleLabel() {
        return new JLabel("Viewing Flights");
    }
}
